<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_from extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
		// $this->fromsource = $this->config->item('fromsource');
		
 		$this->load->model('common/security_model');
 		$this->load->model('admin/admin_from_model');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
		//加载缓存类
		$this->load->library('datacache');
		
	 }

	function index()
	{
		$data['fromsource'] = $this->admin_from_model->get_froms();
		$this->load->template('admin/admin_from_tpl', $data);
	}
	
	function add_from(){
		$data = array();
		$set = $this->input->post('set', true);
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		if ($thevaluesubmit) {
			
			//验证表单
			$insertdata = array(
				'title' => $set['title'],
				'name' => $set['name'],
				'activity_table' => $set['activity_table'],
				'goods_table' => $set['goods_table'],
				'url' => $set['url'],	
				'dateline' => now(),
				);
			$fromid = $this->admin_from_model->insert_from($insertdata);
			
			if ($fromid) {
				//更新主页缓存
				$this->datacache->from_cache();
				showmessage('添加成功','admin/admin_from/index', 3);
			}else{
				showmessage('添加失败','admin/admin_from/index', 3);				
			}
			
		}
		$this->load->template('admin/add_from_tpl', $data);		
	}
	
	function edit_from($fromid){
		$set = $this->input->post('set', true);
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		$fromid = $fromid ? intval($fromid) : 0;
		if (empty($fromid)) {
			showmessage('非法来源');
		}
		$from = $this->admin_from_model->get_from_by_fromid($fromid);
		//表单提交
		if (!empty($thevaluesubmit) && !empty($fromid)) {
			//验证表单
			$updatedata = array(
				'fromid' => $fromid,
				'title' => $set['title'],
				'name' => $set['name'],
				'activity_table' => $set['activity_table'],
				'goods_table' => $set['goods_table'],
				'url' => $set['url']
				);
			$id = $this->admin_from_model->update_from($updatedata);
			//更新主页缓存
			$this->datacache->from_cache();
			showmessage('编辑成功','admin/admin_from/index', 3);
		}
		
		$data['thevalue'] = $from;
		$this->load->template('admin/edit_from_tpl', $data);	
	}
}
?>